package com.xbai.security.security;

import com.xbai.common.utils.MD5;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * 密码处理工具类
 * @author xbai
 * @Date 2021/4/25
 */
@Component
public class DefaultPasswordEncoder implements PasswordEncoder {

    public DefaultPasswordEncoder() {
        this(-1);
    }

    public DefaultPasswordEncoder(int strength) {
    }

    /**
     * 进行 MD5 加密
     * @param charSequence 明文
     * @return 密文
     */
    @Override
    public String encode(CharSequence charSequence) {
        return MD5.encrypt(charSequence.toString());
    }

    /**
     * 进行密码比对
     * @param charSequence 传入的密码
     * @param encodedPassword 加密之后的密码
     * @return 是否匹配
     */
    @Override
    public boolean matches(CharSequence charSequence, String encodedPassword) {
        return encodedPassword.equals(MD5.encrypt(charSequence.toString()));
    }

    public static void main(String[] args) {
        System.out.println(new DefaultPasswordEncoder().encode("111111"));
        System.out.println(MD5.encrypt("111111"));
    }
}
